package cn.pw.test.com.conversion.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import cn.pw.test.com.conversion.R;
import cn.pw.test.com.conversion.view.ConversionInputView;
import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

/**
 * 豆油换算
 */
public class ConversionBeanOilFragment extends BaseFragment {

    private ConversionInputView inFreight;//运费
    private ConversionInputView inTaxRate;//税率
    private ConversionInputView inTake;//基差
    private ConversionInputView inExchangeRate;//汇率
    private ConversionInputView inChicagoPrice;//芝加哥面板价
    private ConversionInputView inResult1;//结果1
    private ConversionInputView inResult2;//结果2

    private TextView calculationTx, clearTx;

    public static ConversionBeanOilFragment newInstance() {
        ConversionBeanOilFragment fragment = new ConversionBeanOilFragment();
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_conversion_bean_oil;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        inFreight = mContentView.findViewById(R.id.conversion_input_freight);
        inTaxRate = mContentView.findViewById(R.id.conversion_input_tax_rate);
        inTake = mContentView.findViewById(R.id.conversion_input_take);
        inExchangeRate = mContentView.findViewById(R.id.conversion_input_exchange_rate);
        inChicagoPrice = mContentView.findViewById(R.id.conversion_input_chicago_price);
        inResult1 = mContentView.findViewById(R.id.conversion_input_result1);
        inResult2 = mContentView.findViewById(R.id.conversion_input_result2);

        calculationTx = mContentView.findViewById(R.id.conversion_button_positive);
        clearTx = mContentView.findViewById(R.id.conversion_button_negative);

        calculationTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculation();
            }
        });

        clearTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanUp(inFreight, inTaxRate, inTake, inExchangeRate, inChicagoPrice, inResult1, inResult2);
            }
        });
    }

    private void calculation() {
        String etFreight = inFreight.getInputEd().getText().toString().trim();
        String etBasisDifference = inTake.getInputEd().getText().toString().trim();
        String etTaxRate = inTaxRate.getInputEd().getText().toString().trim();
        String etExchangeRate = inExchangeRate.getInputEd().getText().toString().trim();
        String etChicagoPrice = inChicagoPrice.getInputEd().getText().toString().trim();

        if (DataUtils.isEmpty(etFreight)) {
            ToastUtil.show(R.string.conversion_string_input_freight_hint);
            return;
        }

        if (DataUtils.isEmpty(etBasisDifference)) {
            ToastUtil.show(R.string.conversion_string_input_basis_hint);
            return;
        }

        if (DataUtils.isEmpty(etTaxRate)) {
            ToastUtil.show(R.string.conversion_string_input_tax_rate_hint);
            return;
        }

        if (DataUtils.isEmpty(etExchangeRate)) {
            ToastUtil.show(R.string.conversion_string_input_exchange_hint);
            return;
        }

        if (DataUtils.isEmpty(etChicagoPrice)) {
            ToastUtil.show(R.string.conversion_string_chicago_hint);
            return;
        }

        double freight = Double.valueOf(etFreight);
        double basisDifference = Double.valueOf(etBasisDifference);
        double taxRate = Double.valueOf(etTaxRate);
        double exchangeRate = Double.valueOf(etExchangeRate);
        double chicagoPrice = Double.valueOf(etChicagoPrice);

        //        美国离岸成本=（芝加哥盘面价+基差）*22.0462
        //        中国到岸成本=((芝加哥盘面价+基差）*22.0462+运费）*汇率*税率
        double result1 = (chicagoPrice + basisDifference) * 22.0462;
        double result2 = (result1 + freight) * exchangeRate * taxRate;

        setResult1(DataUtils.getAmountValue(result1),DataUtils.getAmountValue(result2));
    }

    private void setResult1(String... result) {
        inResult1.getInputEd().setText(result[0]);
        inResult2.getInputEd().setText(result[1]);
    }

    private void cleanUp(ConversionInputView... view) {
        for (ConversionInputView conversionInputView : view) {
            conversionInputView.getInputEd().setText("");
        }
    }
}
